package com.qifan.energy.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.R;
import com.qifan.energy.adapter.SearchListAdapter;
import com.qifan.energy.bean.result.Page2;
import com.qifan.energy.bean.result.SearchItem;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.dialog.LoadingUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.search_et)
    EditText searchEt;

    private List<SearchItem> dataList = new ArrayList();
    private SearchListAdapter adapter;
    private Integer pageNum = 1;//默认0

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        /* 适配器设置 */
        adapter = new SearchListAdapter(dataList);
        adapter.setOnItemClickListener((a, v, p) -> {
            /* 跳转详情页面 */
            SearchItem item = (SearchItem)a.getData().get(p);
            Intent intent = new Intent();
            intent.putExtra(Keys.DETAIL_ID_KEY,item.getId());
            IntentUtils.startActivity(this, GoodsDetailActivity.class,intent);
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        /* 加载更多 */
        adapter.setOnLoadMoreListener(()->{
            productSearchWithKeywordMore();
        },recyclerView);
    }

    @Override
    protected void initData() {
        /* 首页刷新 */
        productSearchWithKeywordRefresh();
    }

    /* 下来刷新 */
    private void productSearchWithKeywordRefresh(){
        pageNum = 1;
        LoadingUtils.show(this);
        String keyword = searchEt.getText().toString().trim();
        NetUtils.productSearch(keyword, pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<SearchItem>>(this) {
            @Override
            public void onSuccess(Page2<SearchItem> page2) {
                LoadingUtils.hide();
                if (null != page2) {
                    dataList = page2.getList();
                    adapter.setNewData(page2.getList());
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 上拉加载 */
    private void productSearchWithKeywordMore(){
        ++pageNum;
        String keyword = searchEt.getText().toString().trim();
        NetUtils.productSearch(keyword, pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<SearchItem>>(this) {
            @Override
            public void onSuccess(Page2<SearchItem> page2) {
                if (null != page2) {
                    dataList.addAll(page2.getList());
                    boolean isNoMore = (dataList.size() >= page2.getTotal());
                    adapter.setEnableLoadMore(!isNoMore);
                    if (isNoMore) {
                        adapter.loadMoreEnd();
                    } else {//刷新数据
                        adapter.setNewData(dataList);
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.back_iv)
    void onBackIvClicked(){
        ActivityStackUtil.finishTopActivity();
    }

    @OnClick(R.id.search_tv)
    void onSearchTvClicked(){
        /* 隐藏键盘 */
        KeyboardUtils.hideSoftInput(this);
        /* 根据关键字搜索 */
        productSearchWithKeywordRefresh();
    }

    @OnEditorAction(R.id.search_et)
    boolean OnSearchEditorAction(TextView v, int actionId, KeyEvent event){
        if(actionId == EditorInfo.IME_ACTION_SEARCH){
            /*ToastUtils.showShort("开始搜索！！！");*/
            /* 根据关键字搜索 */
            productSearchWithKeywordRefresh();
            /* 隐藏键盘 */
            KeyboardUtils.hideSoftInput(this);
        }
        return false;
    }
}
